﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DbAccessTester
{
   public partial class MainForm : Form
   {
      public ConnectionModel ConnectionModel { get; set; }

      #region Form Methods
      
      public MainForm()
      {
         InitializeComponent();
         this.ConnectionModel = new ConnectionModel();
      }

      private void chkMaskPwd_CheckedChanged(object sender, EventArgs e)
      {
         this.txtPWD.PasswordChar = ((sender as CheckBox).Checked) ? '*' : '\0';
      }

      private void btnExecSql_Click(object sender, EventArgs e)
      {
         this.ExecuteSql();
      }

      private void btnCheckConnection_Click(object sender, EventArgs e)
      {
         this.ExecuteConnection();
      }

      private void copyConnectionStringToolStripMenuItem_Click(object sender, EventArgs e)
      {
         System.Windows.Forms.Clipboard.SetText(this.txtConnectionString.Text);
      }

      private void btnCheckConnectionWString_Click(object sender, EventArgs e)
      {
         this.ExecuteConnection();
      }

      #endregion

      #region Custom Methods

      private void InitializeVariables()
      {
         this.ConnectionModel.Server = this.txtServer.Text;
         this.ConnectionModel.Database = this.txtDatabase.Text;
         this.ConnectionModel.UID = this.txtUID.Text;
         this.ConnectionModel.PWD = this.txtPWD.Text;
         this.ConnectionModel.Sql = this.txtSql.Text;
         if(this.ConnectionModel.HasFullConnectionString)
            this.ConnectionModel.ConnectionString = this.txtConnectionString.Text;
         this.dgvMain.DataSource = null;
         this.dgvMain.Refresh();
         this.txtExceptionText.Text = string.Empty;
         this.txtExceptionText.Refresh();
         this.lblStatus.BackColor = Color.LightYellow;
         this.lblStatus.Text = "   checking connection / command, please wait............  ";
         System.Threading.Thread.Sleep(500);
         this.lblStatus.Refresh();
      }

      private void DoPostProcessing()
      {
         if (this.ConnectionModel.HasError)
         {
            this.dgvMain.Visible = false;
            this.txtExceptionText.Visible = true;
            this.txtExceptionText.Text = this.ConnectionModel.ErrorText;
            this.lblStatus.Text = "* * * Connection / Command Failed * * *";
            this.lblStatus.BackColor = Color.Red;
         }
         else
         {
            this.dgvMain.Visible = true;
            this.txtExceptionText.Visible = false;
            this.txtExceptionText.Text = string.Empty;
            this.lblStatus.Text = "Connection / Command Successful!";
            this.lblStatus.BackColor = Color.Lime;
            if(!this.ConnectionModel.HasFullConnectionString)
               this.txtConnectionString.Text = this.ConnectionModel.ConnectionString;
         }
      }

      private void ExecuteConnection()
      {
         this.InitializeVariables();
         this.ConnectionModel.CheckConnection();
         this.DoPostProcessing();
      }

      private void ExecuteSql()
      {
         this.InitializeVariables();
         DataSet dataSet = this.ConnectionModel.ExecuteDataSet();
         this.DoPostProcessing();
         if (dataSet != null && dataSet.Tables.Count != 0 && dataSet.Tables[0].Rows.Count != 0 && dataSet.Tables[0].Columns.Count != 0)
         {
            this.dgvMain.DataSource = dataSet.Tables[0].DefaultView;
            this.dgvMain.Refresh();
         }
      }

      #endregion

      private void inputConnectionStringToolStripMenuItem_Click(object sender, EventArgs e)
      {
         ToolStripMenuItem current = sender as ToolStripMenuItem;
         current.Checked = !current.Checked;
         ConnectionModel.HasFullConnectionString = current.Checked;
         ToggleSimpleConnectionString(current.Checked);
      }

      private void ToggleSimpleConnectionString(bool enable)
      {
         this.ConnectionModel.HasFullConnectionString = enable;

         this.btnCheckConnectionWString.Visible = enable;
         this.lblConnectionStringSimple.Visible = enable;
         this.txtConnectionString.Visible = enable;

         this.btnCheckConnection.Visible = !enable;
         
         this.lblDbName.Visible = !enable;
         this.lblPWD.Visible = !enable;
         this.lblServer.Visible = !enable;
         this.lblUID.Visible = !enable;

         this.txtDatabase.Visible = !enable;
         this.txtPWD.Visible = !enable;
         this.txtServer.Visible = !enable;
         this.txtUID.Visible = !enable;

         this.pnlConnStrBreakup.Visible = true;
         this.pnlConnStrBreakup.Refresh();
         this.pnlConnStrBreakup.PerformLayout();
      }
   }
}
//Copyright Binoj V Antony © 2011 under GNU Library General Public License (LGPL) at http://DbAccessTesterClient.CodePlex.com/
//Please preserve the authors names and copyright statement in the code even when the work is changed or added to.